package com.yunxin.admin.system.api.system;

import com.yunxin.admin.system.application.DeptApplicationService;
import com.yunxin.admin.system.application.DeptQueryService;
import com.yunxin.admin.system.application.assembler.DeptAssembler;
import com.yunxin.admin.system.application.command.AddDeptCommand;
import com.yunxin.admin.system.application.command.DeptCommand;
import com.yunxin.admin.system.application.command.UpdateDeptCommand;
import com.yunxin.admin.system.application.dto.DeptDTO;
import com.yunxin.admin.system.application.dto.DeptInfoDTO;
import com.yunxin.admin.system.application.dto.OptionDTO;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.syslog.AutoLog;
import com.yunxin.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 *
 * @author zero°
 * @since 2023-05-22
 */
@Tag(name = "05.部门接口")
@RestController
@RequestMapping("/api/v1/dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final DeptApplicationService deptService;

    private final DeptQueryService deptQueryService;

    private final DeptAssembler deptAssembler;

    @Operation(summary = "获取部门列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping
    public Result<List<DeptDTO>> listDepartments(@ParameterObject DeptCommand command) {
        List<DeptDTO> list = deptQueryService.listDepartments(command);
        return Result.success(list);
    }

    @Operation(summary = "获取部门下拉选项", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/options")
    public Result<List<OptionDTO<Long>>> listDeptOptions() {
        List<OptionDTO<Long>> list = deptQueryService.listDeptOptions();
        return Result.success(list);
    }

    @Operation(summary = "获取部门表单数据", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/{deptId}/form")
    public Result<DeptInfoDTO> getDeptInfo(@Parameter(description = "部门ID") @PathVariable Long deptId) {
        DeptInfoDTO deptInfoDTO = deptQueryService.getDeptInfo(deptId);
        return Result.success(deptInfoDTO);
    }

    @Operation(summary = "新增部门", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:dept:add')")
    @Resubmit
    @AutoLog(value = "新增部门")
    public Result<Long> saveDept(@Valid @RequestBody AddDeptCommand command) {
        Long id = deptService.saveDept(command);
        return Result.success(id);
    }

    @Operation(summary = "修改部门", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PutMapping(value = "/{deptId}")
    @PreAuthorize("@ss.hasPerm('sys:dept:edit')")
    @AutoLog(value = "修改部门")
    public Result<Long> updateDept(@PathVariable Long deptId, @Valid @RequestBody UpdateDeptCommand command) {
        command.setId(deptId);
        deptId = deptService.updateDept(command);
        return Result.success(deptId);
    }

    @Operation(summary = "删除部门", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/{ids}")
    @AutoLog(value = "删除部门")
    @PreAuthorize("@ss.hasPerm('sys:dept:edit')")
    public Result<Boolean> deleteDepartments(@Parameter(description = "部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids) {
        boolean result = deptService.deleteByIds(ids);
        return Result.judge(result);
    }

}
